package com.library.controller.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@WebFilter("/*")
public class LoginFilter implements Filter {

 
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse resp = (HttpServletResponse) response;
		System.out.println("执行了  登录校验过滤器");
		String uri = req.getRequestURI();
		if(uri.contains("index.jsp") 
				|| uri.equals("/library/")
				|| uri.contains("login.jsp") 
				|| uri.contains("login.do") 
				|| uri.contains("register.jsp")
				|| uri.contains("register.do")
				|| uri.endsWith("css")
				|| uri.endsWith("js")
				)
		{
			chain.doFilter(request, response);
		}else {
			if(req.getSession().getAttribute("loginUser") != null) {
				chain.doFilter(request, response);
			}else {
				resp.sendRedirect("/library/login.jsp");
			}
		}
	}

}
